import Cookie from 'js-cookie'
export default {
    namespaced:true,
    mutations:{
        selectMenu(state,val) {
            if (val.name!=='home') {
                state.currentMenu = val
                const result = state.tabsList.findIndex(item=>item.name === val.name)
                if (result === -1) {
                    state.tabsList.push(val)
                } else {
                    state.currentMenu = null
                }
            }
        },
        delMenu(state,val) {
            const result = state.tabsList.findIndex(item=>item.name===val)
            state.tabsList.splice(result,1)
        },
        setMenu(state, val){
            state.menu = val
            // Cookie.set('menu', JSON.stringify(val))
            localStorage.setItem('menu',JSON.stringify(val))
        },
        clearMenu(state){
            state.menu = ''
            // Cookie.remove('menu')
            localStorage.removeItem('menu')
        },
        addMenu(state, router){
            // if(!Cookie.get('menu')){
            //     return
            // }
            if(!localStorage.getItem('menu')) return
            // const menu = JSON.parse(Cookie.get('menu'))
            // state.menu = menu
            const menu = JSON.parse(localStorage.getItem('menu'))
            state.menu = menu
            const menuArray = []
            state.menu.forEach(item => {
                if(item.children) {
                    item.children = item.children.map(item => {
                        item.component = () =>import(`../views/${item.url}`)
                        return item
                    })
                    menuArray.push(...item.children)
                } else {
                    item.component = () =>import(`../views/${item.url}`)
                    menuArray.push(item)
                }
            })
            // 路由的动态添加
            menuArray.forEach(item => {
                router.addRoute('main', item)
            })
        }
    },
    state:{
        tabsList:[
            {
                path:'/',
                name:'home',
                label:'首页',
                icon:'home'
            }
        ],
        currentMenu:null,
        menu:[]
    }
}